package com.hzqy.web.ac.industry;

import com.alibaba.fastjson.JSONObject;
import com.hzqy.service.industry.PensionUserServiceBean;
import com.hzqy.web.vo.industry.pension.OrderVo;
import org.apache.http.protocol.HTTP;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.util.List;

@Controller
@RequestMapping("/industry/itf")
public class YanglaoAction {
    protected final Logger LOG = LoggerFactory.getLogger(getClass());
    @Resource
    private PensionUserServiceBean pensionUserServiceBean;

    @ResponseBody
    @RequestMapping("/receiveOrder")
    public JSONObject impressionMonitor(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //%7B%22serverId%22%3A%22145545156%22%2C%22orderStatus%22%3A1%7D
        // 读取请求内容
        BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
        String line = null;
        StringBuilder sb = new StringBuilder();
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }

        // 将资料解码
        String reqBody = sb.toString();
        String message = URLDecoder.decode(reqBody, HTTP.UTF_8);
        JSONObject json = JSONObject.parseObject(message);
        LOG.debug("回调信息是"+json);
        if((int)json.get("orderStatus")==1){
            //修改订单状态
            pensionUserServiceBean.updateOrderStatus((String) json.get("serverId"),(int)json.get("orderStatus"));
        }else{
            pensionUserServiceBean.updateOrderStatus((String) json.get("serverId"),(int)json.get("orderStatus"));
            OrderVo orderVo = new OrderVo();
            orderVo.setF_or_server_id((String) json.get("serverId"));
            List<OrderVo> orderInfo= pensionUserServiceBean.getOrderInfo(orderVo);
            String serverId=(String) json.get("serverId");
            //如果是一键呼叫，不需要增加次数操作
            if(serverId.indexOf("call")>0||serverId.indexOf("call")==0){

            }else{
                for (int i = 0; i < orderInfo.size(); i++) {
                    OrderVo vo =orderInfo.get(i);
                    if(vo!=null){
                        pensionUserServiceBean.addUserTimes(""+vo.getF_or_name());
                    }
                }
            }
        }
        return json;

    }
}
